package cn.xo68.boot.auth.server.service;


import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.domain.LoginFailLimit;
import cn.xo68.core.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * 登录失败次数限制服务层
 * @author wuxie
 * @version 2019-4-23
 */
@Service
public class LoginFailLimitService {


    @Autowired
    private CacheManager cacheManager;

    private Cache getCache(){
        return cacheManager.getCache(AuthConstants.CACHE_NAME_LOGIN_FAIL);
    }

    public LoginFailLimit get(String userName){
        LoginFailLimit loginFailLimit=getCache().get(userName,LoginFailLimit.class);
        if(loginFailLimit==null){
            loginFailLimit=new LoginFailLimit();
            loginFailLimit.setUserName(userName);
            loginFailLimit.setFirstDate(DateTime.Now().getDate());
            loginFailLimit.setFailTimes(0);
        }
        return loginFailLimit;
    }


    public void put(LoginFailLimit loginFailLimit){
        getCache().put(loginFailLimit.getUserName(),loginFailLimit);
    }

    public void delete(String userName){
        //清除缓存
        getCache().evict(userName);
    }
}
